/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upf.grupE.interftest;

/**
 *
 * @author Oscar
 */
/**
 * A program which filters an image and stores it in JPEG format according to the quality level specified by the user
 * @author Group E
 */

import com.sun.media.jai.codec.BMPEncodeParam;
import java.awt.image.renderable.ParameterBlock;
import java.awt.image.RenderedImage;
import javax.media.jai.JAI;
import javax.media.jai.KernelJAI;
import com.sun.media.jai.codec.JPEGEncodeParam;

public class SaveQuality {

    static RenderedImage image;
    float quality;
    String path;
    String type;

     public  void saveQualityBMP(String path, String newpath) {


      //  this.type=type;
        int auxiliar=path.length();

        BMPEncodeParam encodeParam = new BMPEncodeParam();
    
        ParameterBlock pb = new ParameterBlock();

        try {
            image = JAI.create("fileload", path);
            pb.addSource(image);
        }
        catch (IllegalArgumentException e) {
            System.out.println("ERROR: File "+path+" not found.");
            System.exit(1);
        }

        ParameterBlock pbSave = new ParameterBlock();
        pbSave.addSource(image);
        pbSave.add(newpath);
        pbSave.add("JPEG");
        pbSave.add(encodeParam);

        JAI.create("filestore", pbSave, null);
        System.out.println("Image "+path+" processed and saved. Output image is "+newpath+".");
    }

    public  void saveQualityJPEG(String path, String newpath, float quality) {

     
      //  this.type=type;
        int auxiliar=path.length();
       
        JPEGEncodeParam encodeParam = new JPEGEncodeParam();


        try {
          
            encodeParam.setQuality(quality);
        }
        catch (NumberFormatException e) {
            System.out.println("ERROR: Quality parameter is not a number.");
            System.exit(1);

        }

        ParameterBlock pb = new ParameterBlock();

        try {
            image = JAI.create("fileload", path);
            pb.addSource(image);
        }
        catch (IllegalArgumentException e) {
            System.out.println("ERROR: File "+path+" not found.");
            System.exit(1);
        }

        

        ParameterBlock pbSave = new ParameterBlock();
        pbSave.addSource(image);
        pbSave.add(newpath);
        pbSave.add("JPEG");
        pbSave.add(encodeParam);

        JAI.create("filestore", pbSave, null);
        System.out.println("Image "+path+" processed and saved. Output image is "+newpath+".");
    }


}
